import os
import re

from src.settings import STATIC_ROOT


class ScanFile:
    """
    文件扫描
    """
    regtxt = r'.+?\.txt'  # 检测txt文件
    reglog = r'.+?\.log'
    regCSV = r'.+?\.csv'  # 检测csv

    def __init__(self):
        self.data_path = os.path.join(STATIC_ROOT, 'text')

    def scanLog(self):
        """
        扫描
        :return:
        """
        file_list = []
        for root, dirs, files in os.walk(self.data_path):
            txtre = re.compile(self.reglog)
            for file_name in files:
                is_txt = re.findall(txtre, file_name)
                if is_txt:  # 只扫描.txt文件
                    file_path_name = os.path.join(root, file_name)
                    file_list.append(file_path_name)
        return file_list

    def scan(self):
        """
        扫描
        :return:
        """
        file_list = []
        for root, dirs, files in os.walk(self.data_path):
            txtre = re.compile(self.regtxt)
            for file_name in files:
                is_txt = re.findall(txtre, file_name)
                if is_txt:  # 只扫描.txt文件
                    file_path_name = os.path.join(root, file_name)
                    file_list.append(file_path_name)
        return file_list

    def scanCsv(self):
        """
        扫描
        :return:
        """
        file_list = []
        for root, dirs, files in os.walk(self.data_path):
            txtre = re.compile(self.regCSV)
            for file_name in files:
                is_txt = re.findall(txtre, file_name)
                if is_txt:  # 只扫描.txt文件
                    file_path_name = os.path.join(root, file_name)
                    file_list.append(file_path_name)
        return file_list

    def scan_test(self):
        for root, dirs, files in os.walk(self.data_path):
            print('当前目录: ', root)
            print('子目录列表: ', dirs)
            print('文件列表: ', files)
            print("--------------------")


if __name__ == '__main__':
    list = ScanFile().scan()
    print(list)
